/**
 * Copyright SunEEE Limited (c) 2014. All rights reserved.
 * This software is proprietary to and embodies the confidential
 * technology of SunEEE Limited.  Possession, use, or copying
 * of this software and media is authorized only pursuant to a
 * valid written license from SunEEE or an authorized sublicensor.
 */
package com.base.context.processor.resource;

import java.util.HashMap;
import java.util.Map;

import com.base.context.AnnotationProcessor;
import com.base.core.error.ErrorUtils;
import com.base.core.error.GenericError;
import com.base.core.type.JsonUtils;
import com.base.core.util.JsonNodeUtils;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.node.ObjectNode;


/**
 * ZhiLi: Change to the actual description of this class
 * 
 * @version Revision History
 * 
 *          <pre>
 * Author     Version       Date        Changes
 * ZhiLi      1.0           2014年11月12日     Created
 *
 * </pre>
 * @since B2B1.0.0
 */
@Service
public class ResourceGroupProcessor implements AnnotationProcessor<ResourceGroup> {
    
    public static Map<String, Class<?>> typeAlias = new HashMap<String, Class<?>>();
    
    public boolean processAnnotation(Class<?> type, ResourceGroup annotation) {
        String group = annotation.key();
        String name = annotation.value();

        ObjectNode resourceNode = (ObjectNode) JsonNodeUtils.get(group);
        if (resourceNode == null) {
            resourceNode = JsonUtils.mapper().createObjectNode();
            JsonNodeUtils.put(group, resourceNode);
        } else {
            throw ErrorUtils.wrap(GenericError.DUPLICATE_RESOURCE_PATH, group);
        }

        resourceNode.put("_value", name);
        ResourceGroupProcessor.typeAlias.put(group, type);

        Object[] objs = type.getEnumConstants();
        for (Object obj : objs) {
            ResourceEnum enumObj = (ResourceEnum) obj;
            resourceNode.put(enumObj.toString(), enumObj.getValue());
        }
        //枚举不需要通过对象容器获取
        return false;
    }

}
